/*
 * @Author: yangni 920556271@qq.com
 * @Date: 2024-01-14 13:53:51
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2024-03-14 15:38:27
 * @FilePath: /yt-devops/lib/pages/devices/device_tab/base_info_edit/base_info_edit_controller.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'package:get/get.dart';
import 'package:native_dart_package/loading/loading_util.dart';
import 'package:yt_devops/configs.dart';
import 'package:yt_devops/model/device_model.dart';
import 'package:yt_devops/pages/devices/device_tab/basic_info/base/base_basic_model.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/services/device/device_sevices.dart';
import 'package:yt_devops/utils/enums.dart';
import 'package:yt_devops/utils/func_utils.dart';
import 'package:yt_devops/utils/toast_utils.dart';

class BaseInfoEditController extends GetxController {
  DeviceServices deviceServices = Get.find<DeviceServices>();
  List<DeviceInfoItem> infos = [];
  DeviceModel? device;

  @override
  void onInit() {
    if (Get.arguments != null) {
      infos = Get.arguments['modelList'];
      device = Get.arguments['device'];
    }
    super.onInit();
  }

  @override
  void onReady() {}

  @override
  void onClose() {}

  bool get isEnableSave {
    if (device?.name?.isNotEmpty == true) {
      return true;
    } else {
      return false;
    }
  }

  // 更新信息
  void save() => Configs.httpClient.requestWithLoading(() async {
        if (FuncUtils.isEmpty(device!.name)) {
          TotastUtils.tip(Get.context!, title: '设备名称不能为空');
          return;
        } else if (device?.masterSlaveMode != null &&
            device?.masterSlaveMode == MasterSlaveModeType.master &&
            FuncUtils.isEmpty(device!.masterSlaveSystemName)) {
          TotastUtils.tip(Get.context!, title: '主从系统名称不能为空');
          return;
        }
        var param = {
          "name": device!.name, //设备名称
          "deviceId": device!.deviceId, //设备id
          "masterSlaveSystemName": device!.masterSlaveSystemName //主从系统名称
        };
        await deviceServices.updateDeviceInfo(param);
        dismissLoading();
        TotastUtils.success(
          Get.context!,
          title: ResKey.updateSuccessTip.tr,
          onDismiss: () {
            Get.back(result: true);
          },
        );
      });

  void cancelEdit() {
    Get.back(result: false);
  }
}
